﻿using GBlog.Core;
using GBlog.Core.Service;
using System.Web.Security;
using GBlog.Core.Entities;
using System.Web.Script.Serialization;

namespace System.Web.Mvc
{
    public static class ControllerExtensions
    {
        public static void CreateAuthenticationTicket(this GController controller, string username)
        {
            User u = controller.Services<IUserService>().GetByUsername(username);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string userData = serializer.Serialize(u);
            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddHours(8), false, userData);
            string encTicket = FormsAuthentication.Encrypt(authTicket);
            HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
            controller.Response.Cookies.Add(faCookie);
        }
    }
}